import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ok/util/util.dart';
import 'package:flutter_ok/widget/widget.dart';

/// 状态页面
class StateLayout extends StatelessWidget {
  final StateType type;
  final String hintText;
  final _CallBack callback;

  const StateLayout(
      {Key key, @required this.type, this.hintText, this.callback})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        if (type == StateType.loading)
          CupertinoActivityIndicator(radius: 16.0)
        else if (type != StateType.empty)
          Opacity(
            opacity: ThemeUtil.isDark(context) ? 0.5 : 1,
            child: AssetImageLoad(
              'state/${type.img}',
              width: 120,
            ),
          ),
        SizedBox(
          width: double.infinity,
          height: 16.0,
        ),
        Text(
          hintText ?? type.hintText,
          style: Theme.of(context).textTheme.subtitle2.copyWith(fontSize: 14.0),
        ),
        if (type == StateType.network || type == StateType.message)
          OutlinedButton(
            child: Text('点击刷新'),
            style: ButtonStyle(
              shape: MaterialStateProperty.all(
                RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(36)),
                ),
              ),
            ),
            onPressed: () {
              if (callback != null) callback(type);
            },
          ),
        SizedBox(height: 50.0),
        OutlinedButton(onPressed:(){}, child: Text("第二个"))
      ],
    );
  }
}

enum StateType {
  /// 加载中
  loading,

  /// 数据空
  dataEmpty,

  /// 无网络
  network,

  /// 消息
  message,

  /// 空
  empty,
}

extension StateTypeExtension on StateType {
  String get img => <String>['', 'zwsp', 'zwwl', 'zwxx', ''][index];

  String get hintText => <String>[
        '加载中...',
        '暂无数据',
        '暂无网络连接',
        '暂无消息',
        '',
      ][index];
}

typedef _CallBack = void Function(StateType stateType);
